﻿using System;
using System.Collections.Generic;
using System.Text;

namespace KingCat_ValidateCheck
{

    public class ValidateCheck
    {
        private static ValidateCheck v;
        private ValidateCheck() { }

        public static ValidateCheck getInstance()
        {
            if (v == null)
            {
                v = new ValidateCheck();
            }
            return v;
        }

        /// <summary>
        /// 检查用户输入的IP地址跟端口号是否有误
        /// </summary>
        /// <param name="ip">ip地址</param>
        /// <param name="port">端口号</param>
        /// <returns></returns>
        public Boolean ValidateIpAndPort(String ip, String port)
        {
            String serverIp = ip;
            String serverPort = port;
            String[] si = serverIp.Split(new char[] { '.' });
            Boolean flag = true;
            /*********** 检查输入的ip地址格式与端口号是否有误 ********************/
            try
            {
                if (si.Length != 4)
                {
                    flag = false;
                }
                foreach (string s in si)
                {
                    if ((s.Length > 3) || ("".Equals(s)))
                        flag = false;
                }

                if (Convert.ToInt32(serverPort) > 65535 || Convert.ToInt32(serverPort) < 0)
                {
                    flag = false;
                }
            }
            catch (Exception e3)
            {
                flag = false;
            }
            return flag;

        }
    }


}
